package com.gxa.learning.pc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.CourseForMain;
import com.gxa.learning.core.pojo.User;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.core.util.JsonUtil;
import com.gxa.learning.core.util.StringUtil;
import com.gxa.learning.pc.mapper.UserMapper;
import com.gxa.learning.pc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author ChenJ
 * @date 2021/12/06 15:01
 **/
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Resource
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询对应用户所有培训项目列表
     *
     * @param page          页数
     * @param size          数量
     * @param userId        培训人id
     * @param projectName   培训项目名称
     * @param type          培训项目类型
     * @param projectStatus 培训项目状态 0表示未开始、1表示进行中
     * @return 列表
     * @throws Exception 抛出
     */
    @Override
    public PageInfo<Map<String, Object>> getUserProjectList(Integer page, Integer size, String userId, String projectName, String type, String projectStatus) throws Exception {
        boolean userIdNull = StringUtil.isBlankOrNull(userId);
        boolean projectNameNull = StringUtil.isBlankOrNull(projectName);
        boolean projectStatusNull = StringUtil.isBlankOrNull(projectStatus);
        if (page == null) {
            page = 0;
        }
        if (size == null) {
            size = 10;
        }
        if (userIdNull) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_NOT_NULL);
        }
        if (projectNameNull) {
            projectName = null;
        }
        if (projectStatusNull) {
            projectStatus = null;
        }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> userProjectList = userMapper.getUserProjectList(userId, projectName, type, projectStatus);
        return PageInfo.of(userProjectList);
    }


    /**
     * 获取全部学员信息
     *
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> getList(Map<String, Object> map) {
        return userMapper.getList(map);
    }

    /**
     * 根据name模糊搜索得到数据(如果传递管理员 即获取所有管理员)
     *
     * @param name 模糊查询条件
     * @return 返回查询到的集合
     */
    @Override
    public List<Map<String, Object>> selectByMark(String name) {
        return userMapper.selectByMark(name);
    }

    @Override
    public User selectByPhone(String phone) {
        return userMapper.selectByPhone(phone);
    }


    /**
     * 查询当前项目所有课程
     *
     * @param projectId 项目编号
     * @return 列表
     * @throws Exception 抛出
     */
    @Override
    public List<Map<String, Object>> getCourseList(String projectId, String userId) throws Exception {
        boolean blankOrNull = StringUtil.isBlankOrNull(projectId);
        boolean blankOrNull1 = StringUtil.isBlankOrNull(userId);
        if (blankOrNull) {
            projectId = null;
        }
        if (blankOrNull1) {
            throw new BusinessException(ExceptionCode.USER_WRONG);
        }
        return userMapper.getCourseList(projectId, userId);

    }


    @Override
    public List<Map> getRecommend() {
        Set<Long> set = new HashSet<>();
        set.add(1l);
        set.add(2l);
        set.add(6l);
        List<Map> recommend = userMapper.getRecommend(set);
        return recommend;
    }

    @Override
    public Map selectDetailRecommend(Long id) {
        Map map = userMapper.selectDetailRecommend(id);
        return map;
    }

    @Override
    public Map getCount(Long id) {
        Map courseCount = userMapper.getCount(id);
        if (courseCount.get("feedback_degree") != null) {
            courseCount.put("feedback_degree", Integer.valueOf(courseCount.get("feedback_degree").toString()) * 20 / Integer.valueOf(courseCount.get("total").toString()));
            courseCount.put("feedback_rationality", Integer.valueOf(courseCount.get("feedback_rationality").toString()) * 20 / Integer.valueOf(courseCount.get("total").toString()));
            courseCount.put("feedback_practicality", Integer.valueOf(courseCount.get("feedback_practicality").toString()) * 20 / Integer.valueOf(courseCount.get("total").toString()));
            courseCount.put("total", ((Integer.valueOf(courseCount.get("feedback_degree").toString()) + Integer.valueOf(courseCount.get("feedback_rationality").toString()) + Integer.valueOf(courseCount.get("feedback_practicality").toString()))) / 3);
        }
        return courseCount;
    }

    @Override
    public CourseForMain getRecommendCourseDetail(Long id) {

        CourseForMain courseForMain = new CourseForMain();

        Set range = redisTemplate.opsForZSet().range(MainServiceImpl.HOT_COURSE, 0, -1);
        for (Object o : range) {
            courseForMain = JsonUtil.stringToObject(o.toString(), CourseForMain.class);
            if (courseForMain.getId() == id) {
                break;
            }
        }

        return courseForMain;
    }

    @Override
    public Map getCourseCount(Long id) {
        Map courseCount = userMapper.getCourseCount(id);
        if (courseCount.get("feedback_degree") != null) {
            courseCount.put("feedback_degree", Integer.valueOf(courseCount.get("feedback_degree").toString()) * 20 / Integer.valueOf(courseCount.get("total").toString()));
            courseCount.put("feedback_rationality", Integer.valueOf(courseCount.get("feedback_rationality").toString()) * 20 / Integer.valueOf(courseCount.get("total").toString()));
            courseCount.put("feedback_practicality", Integer.valueOf(courseCount.get("feedback_practicality").toString()) * 20 / Integer.valueOf(courseCount.get("total").toString()));
            courseCount.put("total", ((Integer.valueOf(courseCount.get("feedback_degree").toString()) + Integer.valueOf(courseCount.get("feedback_rationality").toString()) + Integer.valueOf(courseCount.get("feedback_practicality").toString()))) / 3);
        }
        return courseCount;
    }

    @Override
    public List<Map> getCourseInRecommend(Long id) {
        List<Map> courseInRecommend = userMapper.getCourseInRecommend(id);
        return courseInRecommend;
    }

    @Override
    public PageInfo<Map> getMyCourse(Map map) {
        if (map.get("page") == null) {
            map.put("page", 1);
        }
        if (map.get("size") == null) {
            map.put("size", 10);
        }
        Integer page = Integer.valueOf(map.get("page").toString());

        Integer size = Integer.valueOf(map.get("size").toString());
        PageHelper.startPage(page, size);
        List<Map> myCourse = userMapper.getMyCourse(map);
        PageInfo<Map> of = PageInfo.of(myCourse);

        return of;
    }

    /**
     * 根据id获取单个学员
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getOne(Long id) {
        return userMapper.getOne(id);
    }
}
